package com.study.example.gameserver.module.scene.controller;

import com.sparrow.event.annotation.EventReceiver;
import com.sparrow.example_message.map.req.*;
import com.sparrow.example_message.map.resp.RespChangeMapPacket;
import com.sparrow.resource.cache.ResourceCache;
import com.sparrow.socket.tcp.anno.PacketHandler;
import com.study.example.gameserver.config.SceneResource;
import com.study.example.gameserver.module.backpack.service.BackPackService;
import com.study.example.gameserver.module.scene.domain.MapState;
import com.study.example.gameserver.module.scene.domain.SceneMap;
import com.study.example.gameserver.module.scene.manager.SceneManager;
import com.study.example.gameserver.module.scene.service.SceneService;
import com.study.example.gameserver.module.player.entity.Player;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * x,z 表示地形平面，y 表示高度
 */
@PacketHandler
@Component
public class SceneController {

    private static Logger logger = LoggerFactory.getLogger(SceneController.class);

    @Autowired
    SceneManager sceneManager;
    @Autowired
    SceneService sceneService;
    @Autowired
    ResourceCache<Integer, SceneResource> sceneResCache;
    @Autowired
    private BackPackService backPackService;

    @EventReceiver
    public void reqMove(Player player, ReqMovePacket req) {
//        logger.info("player:{} 请求移动{}", player.getPlayerId(), JsonUtil.toJSON(req));
        SceneMap sceneMap = sceneManager.getSceneMap(player);
        sceneMap.tell(() -> {
            sceneService.move(sceneMap, player, req);
        });
    }

    @EventReceiver
    public void reqChangeMap(Player player, ReqChangeMapPacket req) {
        logger.info("player:{} 请求切换地图 sceneId:{}", player.getPlayerId(), req.getSceneId());
        SceneResource res = sceneResCache.getResource(req.getSceneId());
        if (res == null) {
            return;
        }
        SceneMap newMap = sceneManager.getSceneMap(req.getSceneId(), 1);
        if (newMap == null) {
            return;
        }
//        if (!player.getMapState().equals(MapState.NORMAL)) {
//            return;
//        }
        Runnable r = () -> {
//            player.setMapState(MapState.WAITING_LOAD);

            RespChangeMapPacket packet = new RespChangeMapPacket();
            packet.setSceneId(req.getSceneId());
            player.sendPacket(packet);
        };
        sceneService.changeMap(player, newMap, r);
    }

    @EventReceiver
    public void reqLoadMap(Player player, ReqLoadMapPacket req) {
//        logger.info("player:{} 请求加载地图", player.getPlayerId());
        SceneMap sceneMap = sceneManager.getSceneMap(player);
        sceneMap.tell(() -> {
            sceneService.loadMap(sceneMap, player);
            player.setMapState(MapState.NORMAL);
        });
    }

    @EventReceiver
    public void reqJump(Player player, ReqJumpPacket req) {
        logger.info("player:{} 请求跳跃", player.getPlayerId());
        SceneMap sceneMap = sceneManager.getSceneMap(player);
        sceneMap.tell(() -> {
            sceneService.jump(sceneMap, player, req);
        });
    }

    @EventReceiver
    public void reqAttack(Player player, ReqAttackPacket req) {
//        logger.info("player:{} 请求攻击", player.getPlayerId());
        SceneMap sceneMap = sceneManager.getSceneMap(player);
        sceneMap.tell(() -> {
            sceneService.attack(sceneMap, player, req);
        });
    }

    @EventReceiver
    public void reqAttackHit(Player player, ReqAttackHitPacket req) {
//        logger.info("player:{} 请求攻击命中", player.getPlayerId());
        SceneMap sceneMap = sceneManager.getSceneMap(player);
        sceneMap.tell(() -> {
            sceneService.attackHit(sceneMap, player, req);
        });
    }

    @EventReceiver
    public void reqPickUp(Player player, ReqPickUpPacket req) {
        logger.info("player{} pickup:{}", player.getPlayerName(), req.getTargetId());
        SceneMap sceneMap = sceneManager.getSceneMap(player);
        sceneMap.tell(() -> {
            sceneService.pickUp(sceneMap, player, req.getEntityType(), req.getTargetId());
        });

    }
}
